# Figure 9

library(tidyverse)

DATA           <- read.csv(file.choose(), header=TRUE)

DATA$FACET     <- factor(DATA$FACET, levels=c("Con Uniform","Con Relaxed","Lib Uniform","Lib Relaxed"))
DATA$LEVEL     <- factor(DATA$LEVEL, levels=0:8)
ANNOTATE       <- data.frame(FACET=c("Con Uniform","Con Relaxed","Lib Uniform","Lib Relaxed"), label=c("p<0.05\nevidence\nof an effect","No p<0.05\nevidence\nof an effect",NA,NA))
ANNOTATE$FACET <- factor(ANNOTATE$FACET, levels=c("Con Uniform","Con Relaxed","Lib Uniform","Lib Relaxed"))

LABELS         <- c("Con Uniform"="Conservative Trump treatment\n\u2013 Uniform model \u2013", "Con Relaxed"="Conservative Trump treatment\n\u2013 Non-uniform model \u2013", "Lib Uniform"="Liberal Trump treatment\n\u2013 Uniform model \u2013", "Lib Relaxed"="Liberal Trump treatment\n\u2013 Non-uniform model \u2013")

theme.z <- theme(
        axis.text.x        = element_text(size=15, color="black", hjust=c(0,1), margin=margin(t=7,r=0,b=5,l=0)),
        axis.text.x.top    = element_blank(),
        axis.text.y        = element_text(size=15, color="black",               margin=margin(r=7, l=7)),
        axis.text.y.right  = element_text(size=15, color="black",               margin=margin(r=7, l=7)),
        axis.ticks.x       = element_blank(),
        axis.ticks.y       = element_blank(),
        axis.title.x       = element_blank(),
        axis.title.y       = element_blank(),
        legend.position    = "none",
        panel.background   = element_rect(size=0.5, color="black", fill="gray95", linetype="solid"),
        panel.border       = element_rect(size=1.8, color="black", fill=NA      , linetype="solid"),
        panel.grid.major.x = element_blank(), 
        panel.grid.major.y = element_blank(),
        panel.grid.minor.x = element_blank(), 
        panel.grid.minor.y = element_blank(),
        panel.spacing.x    = unit(1, "lines"),
        panel.spacing.y    = unit(1, "lines"),
        plot.background    = element_rect(fill="white"),
        plot.caption       = element_text(size=12, hjust=0  , margin=margin(t=10)),
        plot.margin        = unit(c(t=5,r=5,b=5,l=5),"pt"),
        plot.subtitle      = element_text(size=15, hjust=0.5, margin=margin(b=7)),
        plot.title         = element_text(size=20, hjust=0.5, face="bold", margin=margin(t=0,b=12)),
        strip.background   = element_rect(color="black", fill="black"),
        strip.text.x       = element_text(size=18, color="white", face="bold", margin=margin(t=10,r=10,b=10,l=10)))

ggplot(data=DATA, aes(x=LEVEL, y=PE)) +
    facet_wrap(vars(FACET), ncol=2, dir="h", labeller=as_labeller(LABELS)) +
    geom_segment(x=-Inf, y=0, xend=Inf, yend=0, color="gray80") +
    geom_rect(xmin=-Inf, xmax=Inf, ymin=0, ymax=Inf, fill="gray80") +
    geom_errorbar(aes(ymin=CILO, ymax=CIHI), size=1.25, width=0) +
    geom_point(data=filter(DATA, FACET=="Con Uniform" | FACET=="Con Relaxed"), size=4, shape=21, stroke=1.5, color="black", fill="red3") +
    geom_point(data=filter(DATA, FACET=="Lib Uniform" | FACET=="Lib Relaxed"), size=4, shape=21, stroke=1.5, color="black", fill="blue3") +
    geom_text(data=ANNOTATE, aes(x=2.25, y=0.3, label=label), size=5, hjust=0, lineheight=0.9) +
    geom_curve(data=filter(DATA, FACET=="Con Uniform"), aes(x=2, y=0.35, xend=0.9, yend=-0.08), arrow=arrow(length=unit(0.25, "cm")), size=0.05, curvature=0.45) +
    geom_curve(data=filter(DATA, FACET=="Con Uniform"), aes(x=2, y=0.30, xend=1.8, yend=-0.08), arrow=arrow(length=unit(0.25, "cm")), size=0.05, curvature=0.50) +
    geom_curve(data=filter(DATA, FACET=="Con Relaxed"), aes(x=2, y=0.35, xend=1  , yend= 0.18), arrow=arrow(length=unit(0.25, "cm")), size=0.05, curvature=0.40) +
    geom_curve(data=filter(DATA, FACET=="Con Relaxed"), aes(x=2, y=0.30, xend=1.8, yend= 0.10), arrow=arrow(length=unit(0.25, "cm")), size=0.05, curvature=0.50) +
        scale_x_discrete(breaks=c(0,8), labels=c("Lowest\nPolitical\nKnowledge", "Highest\nPolitical\nKnowledge")) +
    scale_y_continuous(limits=c(-0.5,0.5), breaks=seq(-0.4,0.4,0.2), labels=seq(-0.4,0.4,0.2), sec.axis=dup_axis()) +
labs(title="Estimated effect of the...") +
    theme.z

ggsave(file="Figure 9.wmf", width=9.5, height=7.5)
getwd()

